<!--
 * @Descripttion: 轮播图
 * @Author: xiao li
 * @Date: 2020-07-06 12:17:07
 * @LastEditors: xiao li
 * @LastEditTime: 2021-04-29 16:48:37
-->
<template>
    <div class="lb-app-sweep-swiper">
        <top-nav></top-nav>
        <div class="page-main">
            <div class="page-top-operate">
                <lb-button type='primary' @click='addSwiper' icon='el-icon-plus'>添加轮播图</lb-button>
            </div>
            <el-table
            v-loading='loading'
            :data="tableData"
          :header-cell-style="{background:'#f5f7fa',color:'#606266'}"
            style="width: 100%">
              <el-table-column
                label="图片">
                <template slot-scope="scope">
                  <lb-image  :src="scope.row.img" />
                </template>
              </el-table-column>
              <el-table-column
                prop="top"
                label="排序值">
              </el-table-column>
              <el-table-column
                prop='create_time'
                label="创建时间">
                <template slot-scope="scope">
                  <p>{{scope.row.create_time | handleTime(1)}}</p>
                  <p>{{scope.row.create_time | handleTime(2)}}</p>
                </template>
              </el-table-column>
              <el-table-column
                label="是否启用">
                <template slot-scope="scope">
                  <el-switch
                    v-model="scope.row.status"
                    :active-value="1"
                    :inactive-value="0"
                    @change='updateSwiper(scope.row.id,scope.row.status)'>
                  </el-switch>
                </template>
              </el-table-column>
              <el-table-column
                label="操作">
                  <template slot-scope="scope">
                    <div class="table-operate">
                      <lb-button type='primary' size='mini' plain @click="$router.push('/app/sweepPayment/addSwiper?id=' + scope.row.id)">{{$t('action.edit')}}</lb-button>
                      <lb-button type='danger' size='mini' plain @click="delSwiper(scope.row.id)">{{$t('action.delete')}}</lb-button>
                    </div>
                  </template>
              </el-table-column>
          </el-table>
          <lb-page
          :batch='false'
          :page='searchForm.page'
          :pageSize='searchForm.limit'
          :total='total'
          @handleSizeChange='handleSizeChange'
          @handleCurrentChange='handleCurrentChange'
          >
        </lb-page>
        </div>
    </div>
</template>

<script>
export default {
  data () {
    return {
      searchForm: {
        page: 1,
        limit: 10
      },
      dialogForm: {
        top: ''
      },
      loading: false,
      tableData: [],
      total: 0
    }
  },
  activated () {
    this.getTableDataList()
  },
  methods: {
    getTableDataList () {
      this.loading = true
      let {searchForm} = this
      this.$api.getSweepPaymentBannerList(searchForm).then(res => {
        console.log(res)
        this.loading = false
        if (res.code === 200) {
          this.tableData = res.data.data
          this.total = res.data.total
        }
      })
    },
    updateSwiper (id, method) {
      this.$api.updateSweepPaymentBanner({method, id}).then(res => {
        if (res.code === 200) {
          this.$message.success(this.$t(method === 2 ? 'tips.successDel' : 'tips.successOper'))
          if (method === 2) {
            this.searchForm.page = this.searchForm.page < Math.ceil((this.total - 1) / this.searchForm.limit) ? this.searchForm.page : Math.ceil((this.total - 1) / this.searchForm.limit)
            this.getTableDataList()
          }
        } else {
          if (method === 2) return
          this.getTableDataList()
        }
      })
    },
    delSwiper (id) {
      this.$confirm(this.$t('tips.confirmDelete'), this.$t('tips.reminder'), {
        confirmButtonText: this.$t('action.comfirm'),
        cancelButtonText: this.$t('action.cancel'),
        type: 'warning'
      }).then(() => {
        this.updateSwiper(id, 2)
      })
    },
    addSwiper () {
      this.$router.push('/app/sweepPayment/addSwiper')
    },
    handleSizeChange (val) {
      this.searchForm.limit = val
      this.handleCurrentChange(1)
    },
    handleCurrentChange (val) {
      this.searchForm.page = val
      this.getTableDataList()
    }
  },
  filters: {
    handleTime (val, type) {
      let time = type === 1 ? moment(val * 1000).format('YYYY-MM-DD') : type === 2 ? moment(val * 1000).format('HH:mm:ss') : moment(val * 1000).format('YYYY-MM-DD HH:mm:ss')
      return time
    }
  }
}
</script>

<style lang="scss" scoped>
    .lb-app-sweep-swiper{
        width: 100%;
        .page-main{
            width: 100%;
        }
        .dialog-inner{
            width: 100%;
            height: 300px;
            // background: #ccc;
        }
    }
</style>
